package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.adapter.MyGradeAdapter;
import com.auto.learning.bean.GradeBean;
import com.auto.learning.bean.GradeItemBean;
import com.auto.learning.bean.ResponeResult;
import com.auto.learning.event.Handle;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.auto.learning.view.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyGradeActivity extends BaseActivity {
	
	ImageView image_head;
	TextView tv_grade_name,tv_grade_num,tv_grade_need_num;
	SeekBar my_seekbar;
	ListView listview_grade;
	MyGradeAdapter adapter;
	List<GradeItemBean> list = new ArrayList<GradeItemBean>();
	GradeBean bean = new GradeBean();
	
	private Handle handle = new Handle(this){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case HttpRequestHelper.GETDATA_SUCCESS:
				ResponeResult result = (ResponeResult) msg.obj;
				if (result != null) {
					bean = new Gson().fromJson(result.getData(), new TypeToken<GradeBean>(){}.getType());
					if (bean != null) {
						showData(bean);
						if (list != null) {
							list.clear();
							if (bean.getGradeList() != null) {
								list.addAll(bean.getGradeList());
								if (adapter != null) {
									adapter.notifyDataSetChanged();
								}
							}
						}
					}
				}
				break;
			case HttpRequestHelper.GETDATA_FAIL:
				CommonMethod.makeNotice(MyGradeActivity.this, msg.obj.toString());
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setViewInLine(R.layout.activity_mygrade);
		initView();
		getData();
		TemporaryVariable.activities.push(this);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
	
	private void initView(){
		setTitle("我的等级");
		image_head = (ImageView) findViewById(R.id.image_head);
		tv_grade_name = (TextView) findViewById(R.id.tv_grade_name);
		tv_grade_num = (TextView) findViewById(R.id.tv_grade_num);
		tv_grade_need_num = (TextView) findViewById(R.id.tv_grade_need_num);
		my_seekbar = (SeekBar) findViewById(R.id.my_seekbar);
		listview_grade = (ListView) findViewById(R.id.listview_grade);
		adapter = new MyGradeAdapter(this, list);
		listview_grade.setAdapter(adapter);
	}
	
	private void showData(GradeBean bean){
		if (bean == null) {
			return;
		}
		ImageLoader.getInstance().displayImage(bean.getGradeImgUrl(), image_head);
		tv_grade_name.setText(bean.getUserGrade());
		tv_grade_need_num.setText(String.format("升级还需阅读%s本书", bean.getNextGradeNeed()+""));
		my_seekbar.setMax(bean.getBookNum() + bean.getNextGradeNeed());
		my_seekbar.setProgress(bean.getBookNum());
	}
	
	private void getData(){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(HttpRequestHelper.USERID, String.valueOf(SharedPreferencesUtils.getParam(this, HttpRequestHelper.USERID, 0))));
		HttpRequestHelper.executeHttpData(this, HttpRequestHelper.MY_GRADE_INFO, params, handle, HttpRequestHelper.GETDATA_SUCCESS, HttpRequestHelper.GETDATA_FAIL);
	}
	
}
